import React, { Component } from 'react';

class TodoHeader extends Component {
  state = {
    name: ''
  }
  render() {
    return (
      <div>
        <header className="header">
          <h1>todos</h1>
          <input
            value={this.state.name}
            onChange={(e) => this.setState({ name: e.target.value })}
            onKeyUp={this.addTodo}
            className="new-todo"
            placeholder="What needs to be done?"
            autoFocus />
        </header>
      </div>
    );
  }
  addTodo = (e) => {
    if (e.keyCode !== 13) return
    if (!this.state.name.trim()) {
      return alert('不能为空')
    }
    this.props.addTodo(this.state.name)
    this.setState({
      name: '',
    })
  }
}

export default TodoHeader;
